<template>
	<view style="min-height: 100vh;background-color: #fff;">
		<dev-navbar :title="title"></dev-navbar>
		<view class="content-information">
			<view class="recommendation">
				根据测试结果，为您推荐如下每日健康打卡任务：
			</view>
			<!-- 每一项 -->
			<view class="each-task" v-for="(item,index) in list" :key="index">
				<view class="project-title justify-content-l">
					<view class="entry-name">
						{{item.taskLibraryName}}
					</view>
					<view class="align-items-l">
						<view class="style-i"></view>
						<view class="style-ii"></view>
						<view class="style-ii"></view>
					</view>
				</view>
				<view class="card-contents" :style="{'background-image':`url(${item.taskLibraryIcon})`}">
					<view class="task-details">
						<view class="task-list" v-show="item.taskTarget">
							目标：{{item.taskTarget||''}}
						</view>
						<view class="task-list" v-show="item.num && item.unit &&item.frequence">
							次数：{{item.num||''}}{{item.unit||''}}/{{item.frequence||''}}
						</view>
						<view class="task-list" v-show="item.timeRangeStr">
							打卡时间：{{item.timeRangeStr||''}}
						</view>
					</view>
				</view>
			</view>
		</view>
		<view style="height: 160rpx;"></view>

		<dev-iphone-bottom bgColor="#fff">
			<dev-button width="655" height="90" radius="45" left="47" top="20" bottom="20" @tapDev="joinPlanButton()">
				加入计划
			</dev-button>
		</dev-iphone-bottom>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: '血压管理计划',
				list: [],
				userTaskId: 1,
				thematicPlan: '',
			};
		},
		onLoad(e) {
			console.info(e,"rrrr--->")
			if (e.userTaskId) {
				this.userTaskId = e.userTaskId;
			}
			if (e.thematicPlanName ||e.healthPlanName) {
				this.title = e.thematicPlanName || e.healthPlanName;
			}
			if (e.thematicPlan) {
				this.thematicPlan = e.thematicPlan;
			}
			this.userTaskDetail();
		},
		methods: {
			joinPlanButton() {
				this.$api.get(global.apiUrls.joinPlanStatus, {
					userTaskId: this.userTaskId,
					thematicPlan: this.thematicPlan,
					step: 2,
				}).then(res => {
					if (res.data.code == 1000) {
						this.joinPlan();
					} else {
						let _this = this;
						uni.showModal({
							title: '',
							cancelText: "否", // 取消按钮的文字
							confirmText: "是", // 确认按钮的文字
							content: res.data.message,
							success: function(res) {
								if (res.confirm) {
									_this.joinPlan();
								} else if (res.cancel) {
									console.log('用户点击取消');
								}
							}
						});
					}
				})
			},
			joinPlan() {
				let _this = this;
				this.$api.post(global.apiUrls.joinPlan, {
					userTaskId: this.userTaskId,
					thematicPlan: this.thematicPlan,
				}).then(res => {
					// this.$message.info(res.data.message)
					if (res.data.code === 1000) {
						uni.showModal({
							title: '提示',
							content: `${res.data.result}`,
							showCancel:false,
							success: function(res) {
								if (res.confirm) {
									_this.$urouter.navigateTo({
										url: '/pagesHome/health-assessment/health-task',
										params: {

										}
									});
								} else if (res.cancel) {
									console.log('用户点击取消');
								}
							}
						});
					}else{
						uni.showToast({
							title: '您已加入该计划',
							icon:'none',
							duration: 4000
						});
					}
				})
			},
			userTaskDetail() {
				this.$api.get(global.apiUrls.userTaskDetail + '/' + this.userTaskId, {}).then(res => {
					if (res.data.result) {
						let datas = res.data.result;
						for (let i = 0; i < datas.length; i++) {
							if (datas[i].frequence == 'D') {
								datas[i].frequence = '天'
							} else if (datas[i].frequence == 'W') {
								datas[i].frequence = '周'
							} else if (datas[i].frequence == 'M') {
								datas[i].frequence = '月'
							}
							if (datas[i].timeRange) {
								let timeRangeStr = '';
								let arr = JSON.parse(datas[i].timeRange);
								for (let j = 0; j < arr.length; j++) {
									let arrs = [];
									arrs.push(arr[j].startTime)
									arrs.push(arr[j].endTime)
									timeRangeStr = timeRangeStr + arrs.join(',').replace(',', '~') + ', '
								}
								datas[i].timeRangeStr = timeRangeStr.substring(0, timeRangeStr.length - 2);
							}
						}
						this.list = datas
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	.content-information {
		padding: 0rpx 24rpx;

		.recommendation {
			height: 82rpx;
			font-size: 24rpx;
			font-weight: 400;
			color: #F5A623;
			line-height: 82rpx;
		}

		.each-task {
			.project-title {
				.entry-name {
					font-size: 38rpx;
					font-weight: 800;
					color: #242A37;
				}

				.style-i {
					width: 74rpx;
					height: 10rpx;
					background: #2EA7E0;
					border-radius: 5rpx;
				}

				.style-ii {
					width: 24rpx;
					height: 10rpx;
					background: #2EA7E0;
					border-radius: 5rpx;
					opacity: 0.3;
					margin-left: 10rpx;
				}
			}
		}

		.card-contents {
			position: relative;
			margin-top: 20rpx;
			width: 702rpx;
			height: 295rpx;
			background-size: 100% 100%;
			border-radius: 20rpx;
			overflow: hidden;
			margin-bottom: 30rpx;

			.task-details {
				display: flex;
				flex-direction: column;
				justify-content: center;
				padding: 0rpx 34rpx;
				position: absolute;
				width: 702rpx;
				height: 120rpx;
				background: rgba(46, 167, 224, 0.92);
				bottom: 0;

				.task-list {
					font-size: 26rpx;
					font-weight: 400;
					color: #FFFFFF;
				}
			}
		}
	}
</style>
